<%@ page language="java" session="true" errorPage="ElderhealthError.jsp"%>
<%@ page import="java.util.*,java.net.URL,java.net.URLConnection,java.net.URLEncoder" %>
<%@ page import="com.elderhealth.worker.HomecareWorker" %>

<%
/*
 * Source:   DoWorkerSearch.jsp
 * Purpose:  This page is responsible for calling the java necessary to do a query on Workers.
 */
%>

<%
  /* initialize redirect URL string */
   String redirectURL;
   try {
     /* Pull name from the parameters */
	 String firstName = request.getParameter("firstname");
     String lastName = request.getParameter("lastname");
	 
	 /* Make an empty string for any data not passed so database returns a good query */
	 if (firstName == null || firstName.equals("")) 
	 {
	     firstName = " ";
	 }
	 if (lastName == null || lastName.equals(""))
	 {
	     lastName = " ";
	 }
	 
	 /* Remove any old results being passed in the session */
	 session.removeAttribute("results");
	 
	 /* Retrieve Workers matching the criteria */
	 HomecareWorker[] foundWorkers = HomecareWorker.getWorkerByMatch(firstName,lastName);
	 
	 /* Set up url to call with all worker names and ids */
     session.setAttribute("results",foundWorkers);
	 redirectURL = "ShowWorkerSearchResults.jsp";
	 
     response.sendRedirect(redirectURL);
   } catch (Exception e) {
     String errorCode = "1";
     String errorMessage = "Error occurred in DoWorkerSearch.jsp. Error is: " + e.toString();
	 redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
	 response.sendRedirect(redirectURL);
   }
%>
